package com.ww.utils;

import redis.clients.jedis.Jedis;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author y
 * @create 2018-01-19 0:13
 * @desc
 **/
public class ExcelReaderUtil {
    //excel2003扩展名
    public static final String EXCEL03_EXTENSION = ".xls";
    //excel2007扩展名
    public static final String EXCEL07_EXTENSION = ".xlsx";

    /**
     * 每获取一条记录，即打印
     * 在flume里每获取一条记录即发送，而不必缓存起来，可以大大减少内存的消耗，这里主要是针对flume读取大数据量excel来说的
     * @param sheetName
     * @param sheetIndex
     * @param curRow
     * @param cellList
     */
    public static void sendRows(String filePath, String sheetName, int sheetIndex, int curRow, List<String> cellList) {
        /*StringBuffer oneLineSb = new StringBuffer();
        oneLineSb.append(filePath);
        oneLineSb.append("--");
        oneLineSb.append("sheet" + sheetIndex);
        oneLineSb.append("::" + sheetName);//加上sheet名
        oneLineSb.append("--");
        oneLineSb.append("row" + curRow);
        oneLineSb.append("::");
        for (String cell : cellList) {
            oneLineSb.append(cell.trim());
            oneLineSb.append("|");
        }
        String oneLine = oneLineSb.toString();
        if (oneLine.endsWith("|")) {
            oneLine = oneLine.substring(0, oneLine.lastIndexOf("|"));
        }// 去除最后一个分隔符

        System.out.println(oneLine);
        */

       /* String goodsName = cellList.get(0);
        String contactName = cellList.get(2);
        String phoneStr = cellList.get(4);
        String checkTime = cellList.get(24);

        System.out.println(">>" + phoneStr);
        String geoStr = ExcelUtils.getGeo(phoneStr);
        if(geoStr == null && geoStr.length() == 0){
            geoStr = "getStrnull";
        }
//        System.out.println(geoStr);

        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.lpush(geoStr, phoneStr + "," + contactName + "," + goodsName + "," + checkTime);
        jedis.close();*/

    }

    public static void readExcel(String fileName) throws Exception {
        int totalRows = 0;
        if (fileName.endsWith(EXCEL03_EXTENSION)) { //处理excel2003文件
            ExcelXlsReader excelXls=new ExcelXlsReader();
            totalRows =excelXls.process(fileName);
        } else if (fileName.endsWith(EXCEL07_EXTENSION)) {//处理excel2007文件
            ExcelXlsxReader excelXlsxReader = new ExcelXlsxReader();
            totalRows = excelXlsxReader.process(fileName);
        } else {
            throw new Exception("文件格式错误，fileName的扩展名只能是xls或xlsx。");
        }
        System.out.println("发送的总行数：" + totalRows);
    }

    public static void main(String[] args) throws Exception {
//        String path = "/Users/wangwei/soft/1.xlsx";
//        ExcelReaderUtil.readExcel(path);
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        Set<String> allkeys = jedis.keys("*");
        Iterator iterator = allkeys.iterator();
        int i = 0;
        int geoCount = allkeys.size();
        while (iterator.hasNext()){
            String key = (String) iterator.next();
            long count = jedis.llen(key);
            System.out.println(key + " >>>> " + (i + 1) + "/" + geoCount + " rows > " + count);

            FileUtils.appendContent2File("/Users/wangwei/soft/rs.txt", key + " >>> " + count + "\r\n");
            FileUtils.appendContent2File("/Users/wangwei/soft/rs.txt", "---------------------\r\n");

            List<String> ls = jedis.lrange(key, 0, count - 1);
            StringBuffer sb = new StringBuffer();
            System.out.println("ls > " + ls.size());
            for (String s : ls){
                sb.append(s + "\r\n");
            }
            FileUtils.appendContent2File("/Users/wangwei/soft/rs.txt", sb.toString());
            i++;
        }

//        System.out.println(allkeys.size());

        jedis.close();

    }
}